package org.xx.b2d;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.awt.GraphicsDevice;
import java.awt.GraphicsEnvironment;
import java.util.List;

import static java.util.Arrays.asList;
import static java.util.Arrays.stream;
import static org.xx.b2d.WorldSize.S_1280x960;
import static org.xx.b2d.WorldSize.S_1440x1080;
import static org.xx.b2d.WorldSize.S_1600x1200;
import static org.xx.b2d.WorldSize.S_1920x1440;
import static org.xx.b2d.WorldSize.S_2560x1920;
import static org.xx.b2d.WorldSize.S_640x480;
import static org.xx.b2d.WorldSize.S_800x600;
import static org.xx.b2d.WorldSize.S_960x720;

public class DeviceUtils {
    private final static Logger LOGGER = LoggerFactory.getLogger(DeviceUtils.class);

    private final static List<WorldSize> ALL_WORLD_SIZES = asList(
            S_2560x1920,
            S_1920x1440,
            S_1600x1200,
            S_1440x1080,
            S_1280x960,
            S_960x720,
            S_800x600,
            S_640x480
    );

    private DeviceUtils() {
        throw new UnsupportedOperationException();
    }

    public static GraphicsDevice getScreen(
            String screenId
    ) {
        if (screenId == null || screenId.isEmpty()) {
            return GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
        }

        final var screenDevices = GraphicsEnvironment.getLocalGraphicsEnvironment().getScreenDevices();
        if (screenDevices == null || screenDevices.length == 0) {
            throw new DeviceException("No screen found");
        }

        return stream(screenDevices)
                .filter(sd -> screenId.equals(sd.getIDstring()))
                .findAny()
                .orElseThrow(() -> new DeviceException("Cannot find specified screen"));
    }

    public static WorldSize getBestSizeByScreen(
            String screenId
    ) {
        final var screenDevice = getScreen(screenId);

        final var screenWidth = screenDevice.getDisplayMode().getWidth();
        final var screenHeight = screenDevice.getDisplayMode().getHeight();

        return ALL_WORLD_SIZES
                .stream()
                .filter(size -> size.getWidth() < screenWidth && size.getHeight() < screenHeight)
                .findFirst()
                .orElseThrow(() -> new DeviceException("Cannot find suitable size"));
    }

    public static WorldSize getBestSizeByFullScreen(
            String screenId
    ) {
        final var screenDevice = getScreen(screenId);

        final var screenWidth = screenDevice.getDisplayMode().getWidth();
        final var screenHeight = screenDevice.getDisplayMode().getHeight();

        System.out.println(screenWidth + "," + screenHeight);

        return ALL_WORLD_SIZES
                .stream()
                .filter(size -> size.getWidth() <= screenWidth && size.getHeight() <= screenHeight)
                .findFirst()
                .orElseThrow(() -> new DeviceException("Cannot find suitable size"));
    }
}
